//
//  SLLoginViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/18.
//  
  

#import "SLLoginViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import "VerifyCodeInputView.h"


@interface SLLoginViewController ()
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIView *boradView1;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *tipLabel1;
@property (nonatomic, strong) UIView *inputView;
@property (nonatomic, strong) UILabel *areaLabel; //+63
@property (nonatomic, strong) UIView *devider;
@property (nonatomic, strong) UITextField *phone;
@property (nonatomic, strong) SLMoreButton *verifyButton;
@property (nonatomic, strong) UIButton *checkbox;
@property (nonatomic, strong) YYLabel *proxyLabel;
@end

@interface SLLoginViewController ()
@property (nonatomic, strong) UIView *boradView2;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *tipLabel2;
@property (nonatomic, strong) UILabel *countdownLabel;
@property (nonatomic, strong) VerifyCodeInputView *verifyCode;
@property (nonatomic, strong) SLMoreButton *unlockButton;

@property (nonatomic, strong) YYTimer *countdownTimer;
@property (nonatomic, assign) NSInteger countdownValue;
@end

@implementation SLLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.fd_prefersNavigationBarHidden = YES;
    _countdownValue = 60;
    
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.boradView1];
    [self.view addSubview:self.boradView2];
    [self.view addSubview:self.icon];
    [self.view bringSubviewToFront:self.icon];
    [self.boradView1 addSubview:self.tipLabel1];
    [self.boradView1 addSubview:self.inputView];
    [self.boradView1 addSubview:self.areaLabel];
    [self.boradView1 addSubview:self.devider];
    [self.boradView1 addSubview:self.phone];
    [self.boradView1 addSubview:self.verifyButton];
    [self.boradView1 addSubview:self.checkbox];
    [self.boradView1 addSubview:self.proxyLabel];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.bottom.equalTo(self.view);
    }];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(aspectVerticalValue(381-44-20)+SAFE_AREA_INSETS.top);
        make.width.height.mas_equalTo(aspectValue(80));
    }];
    [self.boradView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(aspectValue(20));
        make.top.equalTo(self.icon.mas_centerY);
        make.height.mas_equalTo(aspectValue(303));
    }];
    [self.tipLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.boradView1).offset(aspectValue(30));
        make.top.equalTo(self.boradView1).offset(aspectValue(47));
    }];
    [self.inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView1).inset(aspectValue(27));
        make.top.equalTo(self.tipLabel1.mas_bottom).offset(aspectValue(9));
        make.height.mas_equalTo(aspectValue(47));
    }];
    [self.areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.inputView).offset(aspectValue(15));
        make.centerY.equalTo(self.inputView);
        make.width.mas_equalTo(aspectValue(62));
    }];
    [self.devider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.areaLabel.mas_right).offset(aspectValue(2));
        make.centerY.equalTo(self.areaLabel);
        make.width.mas_equalTo(2);
        make.height.mas_equalTo(aspectValue(30));
    }];
    [self.phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.devider.mas_right).offset(aspectValue(15));
        make.centerY.equalTo(self.areaLabel);
        make.right.equalTo(self.inputView).inset(aspectValue(15));
        make.height.mas_equalTo(aspectValue(40));
    }];
    [self.verifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView1).inset(aspectValue(42));
        make.top.equalTo(self.inputView.mas_bottom).offset(aspectValue(15));
        make.height.mas_equalTo(aspectValue(42));
    }];
    [self.checkbox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.boradView1).inset(60);
        make.top.equalTo(self.verifyButton.mas_bottom).offset(16);
        make.width.height.mas_equalTo(16);
    }];
    [self.proxyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.checkbox.mas_right).offset(aspectValue(12));
        make.top.equalTo(self.checkbox);
        make.right.equalTo(self.boradView1).inset(aspectValue(20));
        make.height.mas_equalTo(aspectValue(97));
    }];
    
    [self.view layoutIfNeeded];
    
    NSString *text = self.proxyLabel.text;
    NSRange range1 = [text rangeOfString:@"Loan Agreement"];
    NSRange range2 = [text rangeOfString:@"Privacy Policy"];
    // 创建富文本
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
    //[attrText setColor:UIColorHex(#1B1B1B) range:range1]; // 设置高亮文本颜色
    [attrText setColor:UIColorHex(#666666) range:NSMakeRange(0, text.length)]; // 设置高亮文本颜色
    [attrText setFont:[UIFont systemFontOfSize:aspectValue(12) weight:700] range:range1];
    [attrText setFont:[UIFont systemFontOfSize:aspectValue(12) weight:700] range:range2];
    [attrText setTextHighlightRange:range1 // 设置高亮范围
                              color:UIColorHex(#1B1B1B)
                    backgroundColor:[UIColor clearColor]
                          tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"点击了高亮文本");
        // 在这里处理点击事件
        [SLRoutePush route:Configuration.LoanAgreementDomain parameter:nil];
    }];
    [attrText setTextHighlightRange:range2 // 设置高亮范围
                              color:UIColorHex(#1B1B1B)
                    backgroundColor:[UIColor clearColor]
                          tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"点击了高亮文本");
        // 在这里处理点击事件
        [SLRoutePush route:Configuration.PrivacyPolicyDomain parameter:nil];
    }];
    self.proxyLabel.attributedText = attrText;
    [self.proxyLabel sizeToFit];
    
    [self.proxyLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(CGRectGetHeight(self.proxyLabel.bounds));
    }];
    
    self.boradView2.hidden = YES;
    
    /// MARK: 第二个面板
    //[self.view addSubview:self.boradView2];
    [self.boradView2 addSubview:self.phoneLabel];
    [self.boradView2 addSubview:self.tipLabel2];
    [self.boradView2 addSubview:self.countdownLabel];
    [self.boradView2 addSubview:self.verifyCode];
    [self.boradView2 addSubview:self.unlockButton];
    [self.boradView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(aspectValue(20));
        make.top.equalTo(self.icon.mas_centerY);
        make.height.mas_equalTo(aspectValue(315));
    }];
    [self.tipLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView2).inset(aspectValue(28));
        make.top.equalTo(self.boradView2).offset(47);
        make.height.mas_equalTo(aspectValue(18));
    }];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tipLabel2);
        make.top.equalTo(self.tipLabel2.mas_bottom).offset(aspectValue(8));
        make.height.mas_equalTo(aspectValue(18));
    }];
    [self.countdownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView2).inset(aspectValue(27));
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(aspectValue(17));
        make.height.mas_equalTo(aspectValue(18));
    }];
    [self.verifyCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView2).inset(aspectValue(27));
        make.top.equalTo(self.countdownLabel.mas_bottom).offset(aspectValue(9));
        make.height.mas_equalTo(aspectValue(60));
    }];
    [self.unlockButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView2).inset(aspectValue(42));
        make.top.equalTo(self.verifyCode.mas_bottom).offset(aspectValue(15));
        make.height.mas_equalTo(aspectValue(54));
    }];
}

#pragma mark - 事件
- (void)updateCountdown {
    self.countdownValue -= 1;
    self.countdownLabel.text = [NSString stringWithFormat:@"Send It Again %ld Seconds Later", self.countdownValue];
    
    if (self.countdownValue <= 0) {
        [self.countdownTimer invalidate];
        self.countdownTimer = nil;
        self.countdownValue = 60;
        self.countdownLabel.text = @"Send It Again";
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self.view endEditing:YES];
}


- (UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [UIImageView new];
        _bgView.backgroundColor = UIColor.whiteColor;
        _bgView.image = [UIImage imageNamed:@"bg_login"];
        _bgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _bgView;
}

- (UIView *)boradView1 {
    if (!_boradView1) {
        _boradView1 = [UIView new];
        _boradView1.backgroundColor = UIColor.whiteColor;
        _boradView1.layer.cornerRadius = aspectValue(20);
    }
    return _boradView1;
}
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [UIImageView new];
        _icon.image = [UIImage imageNamed:@"icon_logo"];
        _icon.layer.cornerRadius = 40;
    }
    return _icon;
}
-(UILabel *)tipLabel1 {
    if (!_tipLabel1) {
        _tipLabel1 = [UILabel new];
        _tipLabel1.text = @"Unlock Wealth Key";
        _tipLabel1.font = [UIFont fontWithName:@"PingFang SC" size:14];
        _tipLabel1.textColor = UIColorHex(#666666);
    }
    return _tipLabel1;
}
-(UIView *)inputView {
    if (!_inputView) {
        _inputView = [UIView new];
        _inputView.backgroundColor = UIColorHex(#FFEDF5);
        _inputView.layer.cornerRadius = 15;
    }
    return _inputView;
}
- (UILabel *)areaLabel {
    if (!_areaLabel) {
        _areaLabel = [UILabel new];
        _areaLabel.text = @"+63";
        _areaLabel.textColor = UIColorHex(#333333);
        _areaLabel.font = aspectDefaultFont(18);
    }
    return _areaLabel;
}
- (UIView *)devider {
    if (!_devider) {
        _devider = [UIView new];
        _devider.backgroundColor = UIColorHex(0x333333);
    }
    return _devider;
}
- (UITextField *)phone {
    if (!_phone) {
        _phone = [UITextField new];
        _phone.placeholder = @"Enter Your Lucky Number";
        _phone.font = aspectDefaultFont(14);
        _phone.keyboardType = UIKeyboardTypeNumberPad;
#ifdef DEBUG
        _phone.text = User.user.account;
#endif
    }
    return _phone;
}
- (SLMoreButton *)verifyButton {
    if (!_verifyButton) {
        _verifyButton = [SLMoreButton new];
        _verifyButton.titleLabel.textColor = UIColorHex(#1B1B1B);
        _verifyButton.titleLabel.text = @"Send Verification Code";
        _verifyButton.imageView.image = [UIImage imageNamed:@"btn_more_white"];
        _verifyButton.titleLabel.font = aspectDefaultFont(16);
        [_verifyButton setBackgroundColor:UIColorHex(#FDC5E0)];
        _verifyButton.layer.cornerRadius = 20;
        _verifyButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _verifyButton.layer.borderWidth = 1.5;
        @weakify(self);
        [_verifyButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            [self.view endEditing:YES];
            if (self.phone.text.length == 0) {
                [self.view makeToast:@"Please Enter Phone Number"];
                return;
            }
            if (!self.checkbox.isSelected) {
                [self.view makeToast:@"Please Check Agreement"];
                return;
            }
            Analysis.login.start = NSDate.date;
            [SMSRequestProvider requestSmsWithPhoneNumber:self.phone.text success:^(BOOL success, NSString *message) {
                self.boradView1.hidden = YES;
                self.boradView2.hidden = NO;
                self.phoneLabel.text = self.phone.text;
                //[self.verifyCode.tf becomeFirstResponder];
                [self.view makeToast:message];
                
                if (self.countdownValue == 60) {
                    self.countdownTimer = [YYTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) repeats:YES];
                }
            } failure:^(YTKRequest * _Nonnull request) {
                [ResponseToast showToastWithRequest:request];
            }];
        }];
    }
    return _verifyButton;
}
- (UIButton *)checkbox {
    if (!_checkbox) {
        _checkbox = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkbox setImage:[UIImage imageNamed:@"btn_check_normal"] forState:UIControlStateNormal];
        [_checkbox setImage:[UIImage imageNamed:@"btn_check_select"] forState:UIControlStateSelected];
        _checkbox.selected = YES;
        @weakify(self);
        [_checkbox jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            self.checkbox.selected = !self.checkbox.isSelected;
        }];
    }
    return _checkbox;
}
- (YYLabel *)proxyLabel {
    if (!_proxyLabel) {
        _proxyLabel = [YYLabel new];
        _proxyLabel.numberOfLines = 0;
        _proxyLabel.text = @"Agree To Our Loan Agreement And Privacy Policy.";
        _proxyLabel.font = aspectDefaultFont(12);
        _proxyLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _proxyLabel;
}


#pragma mark - 验证码面板
- (UIView *)boradView2 {
    if (!_boradView2) {
        _boradView2 = [UIView new];
        _boradView2.backgroundColor = UIColor.whiteColor;
        _boradView2.layer.cornerRadius = aspectValue(20);
    }
    return _boradView2;
}
- (UILabel *)tipLabel2 {
    if (!_tipLabel2) {
        _tipLabel2 = [UILabel new];
        _tipLabel2.text = @"The Verification Code Was Sent To";
        _tipLabel2.font = aspectDefaultFont(14);
        _tipLabel2.textColor = UIColorHex(#666666);
    }
    return _tipLabel2;
}
- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [UILabel new];
        _phoneLabel.text = @"19903486782";
        _phoneLabel.font = aspectDefaultFont(14);
        _phoneLabel.textColor = UIColorHex(#1B1B1B);
    }
    return _phoneLabel;
}
- (UILabel *)countdownLabel {
    if (!_countdownLabel) {
        _countdownLabel = [UILabel new];
        _countdownLabel.text = @"Send It Again 60 Seconds Later";
        _countdownLabel.font = aspectDefaultFont(14);
        _countdownLabel.textAlignment = NSTextAlignmentRight;
        _countdownLabel.textColor = UIColorHex(#999999);
    }
    return _countdownLabel;
}

- (VerifyCodeInputView *)verifyCode {
    if (!_verifyCode) {
        _verifyCode = [VerifyCodeInputView new];
        _verifyCode.number = 6;
        _verifyCode.tf.keyboardType = UIKeyboardTypeNumberPad;
#ifdef DEBUG
        _verifyCode.tf.text = @"202406";
#endif
        @weakify(self);
        _verifyCode.inputFullCallback = ^(NSString * _Nonnull text) {
            @strongify(self);
            NSLog(@"输入满了: %@", text);
            [self login];
        };
    }
    return _verifyCode;
}
- (SLMoreButton *)unlockButton {
    if (!_unlockButton) {
        _unlockButton = [SLMoreButton new];
        _unlockButton.titleLabel.text = @"Unlock My Credit Line Now";
        _unlockButton.titleLabel.textColor = UIColorHex(#1B1B1B);
        _unlockButton.imageView.image = [UIImage imageNamed:@"btn_more_white"];
        _unlockButton.titleLabel.font = aspectDefaultFont(16);
        [_unlockButton setBackgroundColor:UIColorHex(#FDC5E0)];
        _unlockButton.layer.cornerRadius = 20;
        _unlockButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _unlockButton.layer.borderWidth = 1.5;
        @weakify(self);
        [_unlockButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            [self.view endEditing:YES];
            if (self.verifyCode.tf.text.length < 6) {
                return;
            }
            [self login];
        }];
    }
    return _unlockButton;
}

- (void)login {
    @weakify(self);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [LoginRequestProvider requestLoginWithPhoneNumber:self.phone.text code:self.verifyCode.tf.text success:^(LoginModel * _Nonnull model) {
        @strongify(self);
        Analysis.login.end = NSDate.date;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (self.loginCallback) {
            self.loginCallback(YES);
            [self dismissViewControllerAnimated:YES completion:nil];
        } else {
            [SLRoutePush routeMain];
        }
    } failure:^(YTKRequest * _Nonnull request) {
        @strongify(self);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [ResponseToast showToastWithRequest:request];
    }];
}
@end
